package com.syq.controller; /**
 * @ClassName: ${NAME}
 * @Description: TODO
 * @Create by: syq
 * @Date: 4/7/2022 下午8:36
 */


import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

@WebServlet(name = "ImagesServlet", value = "/images/*")
public class ImagesServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String picture = requestURI.substring(requestURI.indexOf("images") + 7);

        String filepath = this.getServletContext().getRealPath("WEB-INF" + File.separator + "images" + File.separator + picture);

        File file = new File(filepath);
        if (!file.exists()){
            return;
        }

        FileInputStream fis = new FileInputStream(file);
        byte[] buf=new byte[1024*4];
        int len;
        ServletOutputStream os = response.getOutputStream();
        while ((len=fis.read(buf))!=-1){
            os.write(buf,0,len);
        }

    }
}
